/**
 * 
 */
package com.mingsoft.weixin.servlet;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import com.mingsoft.util.StringUtil;
import com.mingsoft.weixin.constant.SessionConst;
import com.mingsoft.weixin.entity.WeixinEntity;

/**
 * 铭飞科技
 * Copyright: Copyright (c) 2014 - 2015
 * @author killfen
 * Comments:微信层serlvet基础层
 * Create Date:2015-3-28
 * Modification history:
 */
public class BaseServlet extends com.mingsoft.basic.servlet.BaseServlet{

	/**
	 * 设置session
	 * @param request HttpServletRequest 对象
	 * @param key 键SessionConst里面定义
	 * @param obj 对象
	 */
	protected void setWeixinSession(HttpServletRequest request,SessionConst key,Object obj) {
		if (StringUtil.isBlank(obj.toString())) {
			return;
		}
		request.getSession().setAttribute(key.toString(), obj);
	}

	/**
	 * 获取session
	 * @param request HttpServletRequest 对象
	 * @param key 键SessionConst里面定义
	 */
	protected Object getWeixinSession(HttpServletRequest request,SessionConst key) {
		return request.getSession().getAttribute(key.toString());
	}
	
	/**
	 * 获取微信Id
	 * @param request
	 * @return 微信Id
	 */
	protected int getWeixinId(HttpServletRequest request){
		WeixinEntity weixin = (WeixinEntity) this.getWeixinSession(request,SessionConst.WEIXIN_SESSION);
		return weixin.getWeixinId();
	}
	
	/**
	 * 读取服务器主机ip信息
	 * @return 返回主机IP，异常将会获取不到ip
	 */
	protected String getHostIp() {
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			return addr.getHostAddress().toString();// 获得本机IP
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}	
	
}
